package com.wofish.web.controller.goods;

import com.wofish.common.annotation.Log;
import com.wofish.common.core.controller.BaseController;
import com.wofish.common.core.domain.AjaxResult;
import com.wofish.common.enums.BusinessType;
import com.wofish.common.utils.poi.ExcelUtil;
import com.wofish.framework.util.ShiroUtils;
import com.wofish.goods.domain.GoodsCat;
import com.wofish.goods.dto.GoodsCatZtree;
import com.wofish.goods.service.IGoodsCatService;
import com.wofish.goods.service.IGoodsTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类 信息操作处理
 * 
 * @author wofish
 * @date 2019-04-09
 */
@Controller
@RequestMapping("/goods/goodsCat")
public class GoodsCatController extends BaseController
{
    private String prefix = "goods/goods_cat";
	
	@Autowired
	private IGoodsCatService goodsCatService;

	@Autowired
	private IGoodsTypeService goodsTypeService;

	@RequiresPermissions("goods:goodsCat:view")
	@GetMapping()
	public String goodsCat()
	{
	    return prefix + "/goods_cat";
	}
	
	/**
	 * 查询商品分类列表
	 */
	@RequiresPermissions("goods:goodsCat:list")
	@GetMapping("/list")
	@ResponseBody
	public List<GoodsCat> list(GoodsCat goodsCat)
	{
        List<GoodsCat> goodsCatList = goodsCatService.selectGoodsCatList(goodsCat);
		return goodsCatList;
	}
	
	
	/**
	 * 导出商品分类列表
	 */
	@RequiresPermissions("goods:goodsCat:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsCat goodsCat)
    {
    	List<GoodsCat> list = goodsCatService.selectGoodsCatList(goodsCat);
        ExcelUtil<GoodsCat> util = new ExcelUtil<GoodsCat>(GoodsCat.class);
        return util.exportExcel(list, "goodsCat");
    }
	
	/**
	 * 新增商品分类
	 */
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
	{
		mmap.put("goodsCat", goodsCatService.selectGoodsCatById(parentId));

		mmap.put("goodsTypes", goodsTypeService.listGoodsType());

	    return prefix + "/add";
	}


	/**
	 * 新增保存商品分类
	 */
	@RequiresPermissions("goods:goodsCat:add")
	@Log(title = "商品分类", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(GoodsCat goodsCat)
	{
		goodsCat.setCreateBy(ShiroUtils.getLoginName());
		return toAjax(goodsCatService.insertGoodsCat(goodsCat));
	}

	/**
	 * 修改商品分类
	 */
	@GetMapping("/edit/{catId}")
	public String edit(@PathVariable("catId") Long catId, ModelMap mmap)
	{
		GoodsCat goodsCat = goodsCatService.selectGoodsCatById(catId);
		mmap.put("goodsCat", goodsCat);
        mmap.put("goodsTypes", goodsTypeService.listGoodsType());
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存商品分类
	 */
	@RequiresPermissions("goods:goodsCat:edit")
	@Log(title = "商品分类", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(GoodsCat goodsCat)
	{		
		return toAjax(goodsCatService.updateGoodsCat(goodsCat));
	}
	
	/**
	 * 删除商品分类
	 */
	@RequiresPermissions("goods:goodsCat:remove")
	@Log(title = "商品分类", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(goodsCatService.deleteGoodsCatByIds(ids));
	}


	/**
	 * 选择商品分类树
	 */
	@GetMapping("/selectCatTree/{catId}")
	public String selectDeptTree(@PathVariable("catId") Long catId, ModelMap mmap)
	{
		mmap.put("goodsCat", goodsCatService.selectGoodsCatById(catId));
		return prefix + "/tree";
	}

	/**
	 * 加载部门列表树
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<GoodsCatZtree> treeData()
	{
		List<GoodsCatZtree> ztrees = goodsCatService.selectCatTree(new GoodsCat());
		return ztrees;
	}
	
}
